<%--
Created by IntelliJ IDEA.
User: wang
Date: 2021/3/16/016
Time: 18:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%--添加标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%--在作用域中保存一个当前路径参数ctx--%>
<c:set value="${pageContext.request.contextPath}" var="ctx"/>


<div class="providerAdd">
    <div class="location">
        <strong>你现在所在的位置是:</strong>
        <span>订单新增页面</span>
    </div>
    <form action="#" method="post" id="billAddForm">
        <!--div的class 为error是验证错误，ok是验证成功-->
        <div class="">
            <label for="billId">订单编码：</label>
            <input type="text" name="billCode" id="billId" required/>
            <span id="sp1">*</span>
        </div>
        <div>
            <label for="billName">商品名称：</label>
            <input type="text" name="productName" id="billName" required/>
            <span id="sp2">*</span>
        </div>
        <div>
            <label for="billCom">商品单位：</label>
            <input type="text" name="productUnit" id="billCom" required/>
            <span id="sp3">*</span>

        </div>
        <div>
            <label for="billNum">商品数量：</label>
            <input type="text" name="productCount" id="billNum" required/>
            <span id="sp4">*</span>
        </div>
        <div>
            <label for="money">总金额：</label>
            <input type="text" name="totalPrice" id="money" required/>
            <span id="sp5">*</span>
        </div>
        <div>
            <label>供应商：</label>
            <select name="providerId" id="providerId1">
                <option value="0">---请选择---</option>
                <c:forEach items="${providerList}" var="provider">
                    <option value="${provider.id}"
                            <c:if test="${billUpdate.providerId==provider.id}">selected</c:if>
                    >${provider.proName}</option>
                </c:forEach>
            </select>
            <span id="sp6">*</span>
        </div>
        <div>
            <label>是否付款：</label>
            <input type="radio" name="isPayment" value="1" checked/>未付款
            <input type="radio" name="isPayment" value="2"/>已付款
            <span id="sp7">*</span>
        </div>
        <div class="providerAddBtn">
            <!--<a href="#">保存</a>-->
            <!--<a href="billList.jsp">返回</a>-->
            <input type="button" value="保存" id="btn"/>
            <input type="button" value="返回" onclick="location.href='${ctx}/bill/billList'"/>
        </div>
    </form>
</div>
<%--新增用户--%>
<script type="text/javascript">
    $(function () {
        $("#btn").click(function () {
            var flag1 = false;
            var flag2 = false;
            var flag3 = false;
            var flag4 = false;
            var flag5 = false;
            var flag6 = false;

            //数字正则表达式
            var numberRegist1 = /^[1-9]\d*$/;
            var numberRegist2 = /^[0-9]+(.[0-9]{2})?$/;

            //js验证
            if ($("#billId").val() == null || $("#billId").val() == "" || $("#billId").val() == "null") {
                $("#sp1").html("订单编码不能为空或null").css("color", "red");
            } else {
                var billList =${billList};
                var flag = false;
                for (var i in billList) {
                    if (billList[i].billCode == $("#billId").val()) {
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    $("#sp1").html("该订单编号已存在!").css("color", "red");
                } else {
                    $("#sp1").html("该订单编号符合条件").css("color", "green");
                    flag1 = true;
                }
            }

            if ($("#billName").val() == null || $("#billName").val() == "" || $("#billName").val() == "null") {
                $("#sp2").html("商品名称不能为空或null").css("color", "red");
            } else {
                $("#sp2").html("商品名称符合条件").css("color", "green");
                flag2 = true;
            }

            if ($("#billCom").val() == null || $("#billCom").val() == "" || $("#billCom").val() == "null") {
                $("#sp3").html("商品单位不能为空或null").css("color", "red");
            } else {
                $("#sp3").html("商品单位符合条件").css("color", "green");
                flag3 = true;
            }

            if ($("#billNum").val() == null || $("#billNum").val() == "" || $("#billNum").val() == "null") {
                $("#sp4").html("商品数量不能为空或null").css("color", "red");
            } else if (!numberRegist2.test($("#billNum").val())) {
                $("#sp4").html("商品数量不符合条件(小数点后两位的正实数)").css("color", "red");
            } else {
                $("#sp4").html("商品数量符合条件").css("color", "green");
                flag4 = true;
            }

            if ($("#money").val() == null || $("#money").val() == "" || $("#money").val() == "null") {
                $("#sp5").html("总金额不能为空或null").css("color", "red");
            } else if (!numberRegist2.test($("#money").val())) {
                $("#sp5").html("总金额不符合条件(小数点后两位的正实数)").css("color", "red");
            } else {
                $("#sp5").html("总金额符合条件").css("color", "green");
                flag5 = true;
            }

            if ($("#providerId1").val() == 0) {
                $("#sp6").html("请选择供应商！").css("color", "red");
            } else {
                $("#sp6").html("供应商符合条件").css("color", "green");
                flag6 = true;
            }

            //验证成功
            if (flag1 && flag2 && flag3 && flag4 && flag5 && flag6) {
                var data = $("#billAddForm").serialize();
                //alert(data);
                $.post(
                    ctx + "/bill/billAdd",
                    data,
                    function (result) {
                        //alert(result);
                        if (result > 0) {
                            alert("新增成功！");
                            location.href = "${ctx}/bill/billList";
                        } else {
                            alert("新增失败！");
                        }

                    }, "json"
                );
            } else {
                alert("请根据页面提示信息进行修改！");
            }
        });

    });
</script>
